$().ready(function(){
    let page = {
        p:1,
        num:15,
        type:1,//2为订货列表   1为售货列表
        status:10,
        start_time:'',
        end_time:''
    }
    function init(){
        page.start_time = $('#start').val();
        page.end_time = $('#end').val();
        //获取列表
        getOrderList(page.type,page.p,page.num,page.status,global.community_id,page.start_time,page.end_time);

    }
    init();
    //状态切换
    $('#order_status').on('change',function(){
        let status = $(this).children('option:selected').val();
        console.log(status);
        page.status = status;
    });
    //解锁条件搜索
    $('#searchBtn').on('click',function(){
        page.start_time = $('#start').val();
        page.end_time =  $('#end').val();
        getOrderList(page.type,page.p,page.num,page.status,global.community_id,page.start_time,page.end_time);
    });
    //判断类别
    function statusToString(status){
       let text = status == 0?'未支付':(status == 1?'待发货':(status==2?'待收货':'已完成'));
       return text;
    }
    //模态框初始化
    $('#OrdersModal1').on('show.bs.modal',function(e){
        let modal = $(this);
        let btn = $(e.relatedTarget);
        let id = btn.data('id');
        let list = page.list;
        let order = {};
        for(let i in list){
            if(id == list[i].id){
                order = list[i];
            }
        }
        modal.find('.price').text(order.all_price + '元');
        modal.find('.name').text(order.name);
        modal.find('.phone').text(order.phone);
        modal.find('.address').text(order.sheng+order.shi+order.qu+order.address);
        modal.find('.orderNum').text(order.order_num);
        modal.find('.orderDate').text(setTime2(order.createtime));
        modal.find('.orderStatus').text(statusToString(order.status));
        let html = '';
        for(let j in order.content){
            html += `<div class="shopItemAre">
                <img src="${order.content[j].head_img}" class="shopImg"></img>
                <div class="shopItem">
                    <div>${'产品名称：'+order.content[j].product_name}</div>
                    <div>${'购买个数：'+ order.content[j].num}</div>
                    <div>${'消费金额：'+order.content[j].price}元</div>
                </div>
            </div>`
        }
        modal.find('.shopMainAre').html(html);
    })
    //获取列表
    function getOrderList(type,p,num,status,community_id,start_time,end_time){
        let data = {};
        if(community_id){

        }else{
            community_id = 0;
        }
        let start = '';
        let end = '';
        if(start_time&&end_time){
            start = dateTotimestamp(start_time + ' 00:00:00');
            end = dateTotimestamp(end_time + ' 23:59:59');
        }
        if(type == 1){
            data = {
                type:type,//2为订货列表   1为售货列表
                status:status,//10 全部
                start_time:start,
                end_time:end,
                community_shop_id:community_id,//type:1
                num:num,
                p:p
            }
        }
        if(type == 2){
            data = {
                type:type,//2为订货列表   1为售货列表
                status:status,//10 全部
                start_time:start,
                end_time:end,
                buy_community_id:community_id,
                num:num,
                p:p
            }
        }
        console.log(data);
        getAjax(`${global.apiUrl}/Api/Alssy/get_als_product_order`,res=>{
            console.log(res);
            let html = '';
            let list = res.list;
            if(list.length != 0){
                page.list = list;
                $('#page').paging({
                    pageNo:p,
                    totalSize:res.count,
                    totalPage:Math.ceil(res.count/num),
                    callback(pages){
                        page.p =pages;
                        getOrderList(page.type,page.p,page.num,page.status,global.community_id,page.start_time,page.end_time);
                    }
                });
                for(let i in list){
                    html += `
                        <tr>
                            <td>${list[i].name}</td>
                            <td>${list[i].phone}</td>
                            <td>${list[i].order_num}</td>
                            <td>${setTime2(list[i].createtime)}</td>
                            <td>${list[i].all_price}</td>
                            <td>${list[i].status==0?'未支付':`${list[i].status==1?'待发货':'已完成'}`}</td>
                            <td>
                                <span style="color:#0b67ff;cursor:pointer;" data-target="#OrdersModal1" data-toggle="modal" data-id="${list[i].id}">查看详情</span>
                            </td>
                        </tr>

                     `
                }

            }else{
                $('#page').paging({
                    pageNo:0,
                    totalSize:0,
                    totalPage:0,
                    callback(pages){
                        page.p =pages;
                        getOrderList(page.type,page.p,page.num,page.status,global.community_id,page.start_time,page.end_time);
                    }
                });
                html = `<tr><td colspan="7" >无订货单</td></tr>`
            }
            $('#ordersList1').html(html);
        },data)
    }
});
